const {
	parse
} = require('rss-to-json')
const fs = require('fs')
const path = require('path');
const res = fs.readFileSync('./feed.json')
const feedList = JSON.parse(res.toString('utf-8'))
const express = require('express')
const app = express()
const port = 3555
app.use(express.static('web'))
// 设置模板引擎
app.set('views', path.join(__dirname, 'web'));
app.set('view engine', 'ejs');
const fetchAllFeeds = () => {
	return new Promise((resolve, reject) => {
		const pa = feedList.map((item) => {
			return parse(item.rss)
		})
		Promise.all(pa.map(p => p.catch(e => {
			return {
				title: `订阅异常${e}`,
				items: []
			}
		}))).then(res => {
			resolve(res)
		})
	})
}
app.get('/', async (req, res) => {
	let result = []
	result = await fetchAllFeeds()
	res.render('index', {
		result: result
	});
})
app.get('/feeds/list', async (req, res) => {
	let result = []
	// result = await fetchAllFeeds()
	res.send({
		data: feedList,
		total: feedList.length,
	})
})
app.listen(port, () => {
	console.log(`app listening on port ${port}`)
})